﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http;
using Ewk.MachineLearning.Persistance.DataAccess;
using Ewk.MachineLearning.Persistance.Domain;

namespace Clustering.MultidimensionalScaling.Web.UI.Controllers
{
    public abstract class EntityController<T> : ApiController
        where T : Entity
    {
        // GET api/entity
        public async Task<IEnumerable<T>> GetAsync()
        {
            return await Task.Factory.StartNew(() =>
            {
                using (var catalog = CatalogFactory.NewCatalog())
                {
                    var repository = new EntityRepository<T>(catalog);
                    return repository.Entities;
                }
            });
        }

        // GET api/entity/5
        public async Task<T> GetAsync(Guid id)
        {
            return await Task.Factory.StartNew(() =>
            {
                using (var catalog = CatalogFactory.NewCatalog())
                {
                    var repository = new EntityRepository<T>(catalog);
                    return repository.GetEntityById(id);
                }
            });
        }

        // DELETE api/entity/5
        public async Task DeleteAsync(Guid id)
        {
            await Task.Factory.StartNew(() =>
            {
                using (var catalog = CatalogFactory.NewCatalog())
                {
                    var documentRepository = new EntityRepository<T>(catalog);
                    var document = documentRepository.GetEntityById(id);

                    documentRepository.Delete(document);
                }
            });
        }
    }
}
